package com.cassandra;

import java.io.UnsupportedEncodingException;

import org.apache.cassandra.service.Cassandra;
import org.apache.cassandra.service.ColumnPath;
import org.apache.cassandra.service.ConsistencyLevel;
import org.apache.cassandra.service.InvalidRequestException;
import org.apache.cassandra.service.TimedOutException;
import org.apache.cassandra.service.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.junit.Test;

public class CassandraTest {

	@Test
	public void testSimple(){
		TTransport tr = new TSocket("localhost", 9160);
        TProtocol proto = new TBinaryProtocol(tr);
        Cassandra.Client client = new Cassandra.Client(proto);
        try {
			tr.open();


			String key_user_id = "I的世界";
			 
	        // insert data
	        long timestamp = System.currentTimeMillis();
	        client.insert("Keyspace1",
	                      key_user_id,
	                      new ColumnPath("Standard1", null, "网址".getBytes("UTF-8")),
	                      "http://gpcuster.cnblogs.com".getBytes("UTF-8"),
	                      timestamp,
	                      ConsistencyLevel.ONE);
//	        client.insert("Keyspace1",
//	                      key_user_id,
//	                      new ColumnPath("Standard1", null, "作者".getBytes("UTF-8")),
//	                      "逖靖寒".getBytes("UTF-8"),
//	                      timestamp,
//	                      ConsistencyLevel.ONE);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidRequestException e) {
			e.printStackTrace();
		} catch (UnavailableException e) {
			e.printStackTrace();
		} catch (TimedOutException e) {
			e.printStackTrace();
		} catch (TException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
 
	}
	
	public static void main(String[] args) {
		CassandraTest test = new CassandraTest();
		test.testSimple();
	}
}
